package com.agileboot.attendance.domain.settlement.dto;

import com.agileboot.attendance.domain.settlement.entity.AttSettlementEntity;
import com.agileboot.attendance.domain.settlement.enums.SettlementPeriodType;
import com.agileboot.common.utils.bean.BeanCopyUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 结算单DTO
 */
@Data
@ApiModel("结算单信息")
public class SettlementBillDTO {

    @ApiModelProperty(value = "结算单ID")
    private Long id;

    @ApiModelProperty(value = "结算单号")
    private String billNo;

    @ApiModelProperty(value = "项目ID")
    private Long projectId;

    @ApiModelProperty(value = "项目名称")
    private String projectName;

    @ApiModelProperty(value = "订单ID")
    private Long orderId;

    @ApiModelProperty(value = "订单编号")
    private String orderNo;

    @ApiModelProperty(value = "供应商ID")
    private Long supplierId;

    @ApiModelProperty(value = "供应商名称")
    private String supplierName;

    @ApiModelProperty(value = "结算周期类型")
    private SettlementPeriodType periodType;

    @ApiModelProperty(value = "结算周期类型描述")
    private String periodTypeDesc;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "结算开始日期")
    private LocalDate startDate;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "结算结束日期")
    private LocalDate endDate;

    @ApiModelProperty(value = "结算总金额")
    private BigDecimal totalAmount;

    @ApiModelProperty(value = "结算单状态")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private String status;

    @ApiModelProperty(value = "结算单状态描述")
    private String statusDesc;

    @ApiModelProperty(value = "备注")
    private String remark;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty(value = "创建人")
    private String createBy;

    @ApiModelProperty(value = "结算单明细")
    private List<SettlementBillDetailDTO> details;

    /**
     * 通过实体类构建DTO
     */
    public static SettlementBillDTO fromEntity(AttSettlementEntity entity) {
        if (entity == null) {
            return null;
        }

        SettlementBillDTO dto = BeanCopyUtils.copy(entity, SettlementBillDTO.class);
        
        // 设置ID字段
        dto.setId(entity.getSettlementId());

        // 设置结算周期类型描述
        if (dto.getPeriodType() != null) {
            dto.setPeriodTypeDesc(dto.getPeriodType().getLabel());
        }

        // 设置状态字段和描述 - 使用原始状态值
        if (entity.getStatus() != null) {
            // 设置状态值
            dto.setStatus(String.valueOf(entity.getStatus().getValue()));
            // 设置状态描述
            dto.setStatusDesc(entity.getStatus().getLabel());
        }

        return dto;
    }
} 